//
//  AEButton.cpp
//  GameJam-Mac
//
//  Created by Clover Sin on 12/27/11.
//  Copyright (c) 2011 Snow Poem Society. All rights reserved.
//

#include "stdafx.h"

#include "AEButton.h"

AEButton::AEButton(AEControl* parent, AETheme* theme):
    AEControl(parent, theme), 
    _label(NULL)
{
    // Initialize child first. 
    _label = new AELabel(this, theme);
    // TODO: This value ralated to Font size and text alignment, 
    //   instead of hard-coding.
    _label->setOffsetFromParent(Vector2(0.0f, 8.0f));
    // Label will not take cursor, so event will still 
    //   be handled by button.
    _label->isOccupyingCursor();
    
    _childControls.push_back(_label);
    
    this->SetName("Button");
    this->setText(this->GetName());    // Default text is its name. 
    this->SetSize(_theme->buttonSize);
    this->SetSprite(_theme->buttonNormalTexture);
}

bool AEButton::MouseEnter(Vector2 coor)
{
    // Change texture to mouse hover. 
    this->SetSprite(_theme->buttonMouseHoverTexture);
    return true;
}

bool AEButton::MouseLeave(Vector2 coor)
{
    // Change texture to normal. 
    this->SetSprite(_theme->buttonNormalTexture);
    return true;
}

bool AEButton::MouseHover(Vector2 coor)
{
    // Change texture to mouse hover if mouse is not down.
    if (_state != CS_PressDown)
    {
        this->SetSprite(_theme->buttonMouseHoverTexture);
    }
    return true;
}

bool AEButton::MouseDown(Vector2 coor, MouseButtonInput button)
{
    // Change texture to mouse down. 
    this->SetSprite(_theme->buttonPressDownTexture);
    
    // Occupy all the events.
    
    
    return true;
}

bool AEButton::MouseUp(Vector2 coor, MouseButtonInput button)
{
    // Do nothing by default.
    return true;
}

void AEButton::prepareForAddingToWorld()
{
    syncChildrenPosition();
    
    _label->prepareForAddingToWorld();
    theWorld.Add(_label);
}

bool AEButton::MouseClick(MouseButtonInput button)
{
    // Left click will send message to the delegate.
    if (_delegate && _mouseDownButton == MOUSE_LEFT)
    {
        _delegate(this);
    }
    return true;
}

void AEButton::setText(const String &text)
{
    _label->setText(text);
}

String AEButton::getText()
{
    return _label->getText();
}
